home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Fun with <time.h>
- Date: Wed, 20 Mar 96 21:45:10 GMT
- Organization: none
- Distribution: world
- Message-ID: <827358310snz@genesis.demon.co.uk>
- References: <Pine.A32.3.91.960318145539.118335A-100000@red.weeg.uiowa.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <Pine.A32.3.91.960318145539.118335A-100000@red.weeg.uiowa.edu>
- robinson@blue.weeg.uiowa.edu "The Amorphous Mass" writes:
-
- >
- > I'm writing a little routine that takes a date, a format to parse the
- >date with, and a format to convert the date to, and so I've been sniffing
- >around in <time.h>. One minor annoyance has been that if I'm given a
- >date like 5/6/95 and a format that wants the day (Monday, Tuesday, etc)
- >I couldn't find any functionality to do that. But someone noted in
- >comp.lang.c.moderated that it was possible to pass an incomplete struct
- >tm to mktime() and mktime() would "fill in the blanks" if there was
- >enough information. Is this true, or am I going to have to join the
- >vast and lonely horde of people looking for perpetual calendar
- >algorithms? :-)
-
- mktime() will do what you want in this case. It ignores the original
- values of tm_wday and tm_yday and fills them in calculated from the
- other members of the structure.
-
- > On a side note, what does happen if you pass an incompletely filled
- >struct tm (ie, some fields set to 0) to mktime()? I got some pretty
- >colorful results from DEC C...
-
- You *must* set all of the (standard) struct tm members other than tm_wday
- and tm_yday appropriately before calling mktime(). The values need not
- be within any specific bounds in which case mktime() will adjust the values
- of all of the members to bring them within appropriate ranges, while
- maintaining the same point in time. So, for example, if you want to determine
- what the date is tomorrow you can:
-
- ...
- time_t timeval;
- struct tm *tmptr;
-
- timeval = time(NULL);
- tmptr = localtime(&timeval);
- tmptr->tm_mday++;
- tmptr->is_dst = -1; /* This allows it to choose an appropriate
- daylight-saving-time state and preserve the
- hour value, if possible */
- mktime(tmptr);
-
- And now all of the struct tm fields are set up appropriately for this time
- tomorrow.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-